package com.wisezone.java1902.d13_sort;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Student implements Comparable<Student> {
	private String name; // 姓名
	private int age; // 年龄
	private float score; // 成绩

	public Student(String name, int age, float score) {
		this.name = name;
		this.age = age;
		this.score = score;
	}

	public String toString() {
		return name + "\t" + age + "\t" + score;
	}

	// 返回正数，比较的小数排前面,大数排后面; 升序排
	// 返回负数， 比较的小数排后面,大数排前面;降序排
	@Override
	public int compareTo(Student o) {

		float a = this.score - o.score;
		if (a > 0) {
			return 1; 
		} else if (a < 0) {
			return -1; 
		} else {
			return 0;
		}

	}

	public static void main(String[] args) {
		Student s1 = new Student("小明", 23, 89);
		Student s2 = new Student("小丽", 22, 79);
		Student s3 = new Student("小强", 23, 99);
		Student s4 = new Student("小张", 23, 88);
		Student s5 = new Student("李四", 23, 90);

		// 对以上学生进行排序,按照年龄由低到高排在按成绩由高到低
		List<Student> lists = new ArrayList<Student>();
		lists.add(s1);
		lists.add(s2);
		lists.add(s3);
		lists.add(s4);
		lists.add(s5);

//		Student[] students = (Student[]) lists.toArray();

		for (Student student : lists) {
			System.out.println(student);
		}

		Collections.sort(lists); // 排序

		System.out.println("排序之后");

		for (Student student : lists) {
			System.out.println(student);
		}
	}

}
